import {createRouter, createWebHashHistory} from 'vue-router'
import Layout from '../layout'


const login = {
    hidden: true,
    path: '/login',
    name: 'Login',
    component: () => import('../views/login'),
    meta: {title: '后台', icon: 'Grid'}
}

const routes = [
    login,
    {
        path: '/',
        name: 'Dashboard',
        component: Layout,
        redirect: '/dashboard',
        meta: {title: '首页', icon: 'HomeFilled'},
        children: [
            {
                path: 'dashboard',
                component: () => import( '../views/dashboard'),
                meta: {title: '仪表盘', icon: 'House'}
            },

        ]
    },
    {
        path: '/setting',
        name: 'Setting',
        component: Layout,
        meta: {title: '设置', icon: 'Tools'},
        children: [
            {
                path: 'config',
                component: () => import( '../views/setting'),
                meta: {title: '系统设置', icon: 'Setting'}
            },
            {
                path: 'management',
                component: () => import( '../views/setting/management'),
                meta: {title: '管理员', icon: 'Management'}
            },
            // {
            //     path: 'custom',
            //     component: () => import( '../views/table/custom'),
            //     meta: {title: '表格', icon: 'Management'}
            // },
        ]
    },


    {
        path: '/user',
        name: 'User',
        component: Layout,
        meta: {title: '会员', icon: 'UserFilled'},
        children: [
            {
                path: '',
                component: () => import( '../views/user'),
                meta: {title: '会员列表', icon: 'User'}
            },
            {
                path: 'notify',
                component: () => import( '../views/user/notify'),
                meta: {title: '会员通知', icon: 'User'}
            },
            {
                path: 'verified',
                component: () => import( '../views/user/verified'),
                meta: {title: '认证管理', icon: 'Coordinate'}
            },
            {
                path: 'income',
                component: () => import( '../views/user/income.vue'),
                meta: {title: '收支明细', icon: 'Tickets'}
            },
            {
                path: 'recharge',
                component: () => import( '../views/user/recharge'),
                meta: {title: '支付单', icon: 'Tickets'}
            },
            {
                path: 'withdraw',
                component: () => import( '../views/user/withdraw.vue'),
                meta: {title: '提现记录', icon: 'Tickets'}
            },
        ]
    },
    {
        path: '/shop',
        name: 'Shop',
        component: Layout,
        meta: {title: '店铺', icon: 'TakeawayBox'},
        children: [
            {
                path: '',
                component: () => import( '../views/shop'),
                meta: {title: '店铺列表', icon: 'Tickets'}
            },
            {
                path: 'shop-type',
                component: () => import( '../views/shop/shop-type'),
                meta: {title: '店铺分类', icon: 'Memo'}
            },
            {
                path: 'income',
                component: () => import( '../views/shop/income.vue'),
                meta: {title: '收支明细', icon: 'Coordinate'}
            },
            {
                path: 'recharge',
                component: () => import( '../views/shop/recharge'),
                meta: {title: '充值记录', icon: 'Coordinate'}
            },
            {
                path: 'withdraw',
                component: () => import( '../views/shop/withdraw.vue'),
                meta: {title: '提现记录', icon: 'Tickets'}
            },
        ]
    },
    {
        path: '/activity',
        name: 'Activity',
        component: Layout,
        meta: {title: '活动', icon: 'Discount'},
        children: [
            {
                path: '',
                component: () => import( '../views/activity'),
                meta: {title: '活动列表', icon: 'Tickets'}
            },
            // {
            //     path: 'category',
            //     component: () => import( '../views/activity/category'),
            //     meta: {title: '活动分类', icon: 'Memo'}
            // },
            {
                path: 'ad',
                component: () => import( '../views/activity/ad'),
                meta: {title: '广告列表', icon: 'Memo'}
            }, {
                path: 'home-ad',
                component: () => import( '../views/activity/home-ad'),
                meta: {title: '首页抽奖', icon: 'Memo'}
            },
        ]
    },

    {
        path: '/order',
        name: 'Order',
        component: Layout,
        meta: {title: '订单', icon: 'TakeawayBox'},
        children: [
            {
                path: '',
                component: () => import( '../views/order'),
                meta: {title: '订单列表', icon: 'Tickets'}
            },
            {
                path: 'group',
                component: () => import( '../views/order/group-order'),
                meta: {title: '团购订单', icon: 'Tickets'}
            },
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})


export function permissionMenu(menuIds) {
    console.log('permissionMenu', menuIds)
    // const array = handlerMenu(routes, menuIds)
    return routes
}

// eslint-disable-next-line no-unused-vars
function handlerMenu(list, menuIds) {
    const array = []
    for (let i = 0; i < list.length; i++) {
        const d = list[i]
        if (d.meta && menuIds.includes(d.meta.secured)) {
            if (d.children) {
                const child = handlerMenu(d.children, menuIds)
                d['children'] = child
            }
            array.push(d)
        }
    }
    console.log('权限菜单', array)
    // 返回全部菜单, 需要角色权限时,返回 array, return array;
    return list;
}


export default router

